#include "log/log.h"
#include "smart/smart_drm.hpp"

int main(const int argc, const char* argv[])
{
    if (argc != 4) {
        log_error("para failed, simple: ./tst_run yuyv_4k.yuv 3840 2160");
        exit(EXIT_FAILURE);
    }
    log_debug("SmartDrm drm");
    SmartDrm drm;
    log_debug("SmartDrm drm init");
    drm.init(atoi(argv[2]), atoi(argv[3]));
    log_debug("fopen file");
    FILE* file_fd = fopen(argv[1], "r");

    getchar();
    while (true) {
        getchar();

        printf("--------------- start ----------------\n");
        int ret;
        uint64_t size;
        uint8_t* ptr;
        ptr = drm.get_free_page(&size);
        // if (size != atoi(argv[2])*atoi(argv[3])*2 || ptr == nullptr) {
        //     log_error("drm get free page failed, ptr: %p, size: %ld.", ptr, size);
        //     exit(EXIT_FAILURE);
        // }
        log_debug("fread size[%d]", size);
        ret = fread(ptr, 1, size, file_fd);
        
        if (ret == 0) {
            log_info("file read end.");
            break;  // 读取结束，正常退出
        }
        if (ret == size) {  // 读取正常
            log_info("page_flip.");
            drm.set_page_flip();

            static uint32_t num = 0;
            printf("--------------- end:[%d] ----------------\n", num++);
        } else {
            log_error("file[%s] read length error, len: %d", argv[1], ret);
            exit(EXIT_FAILURE);
        }
    }

    log_debug("normal exit.");
    fclose(file_fd);
    drm.deinit();
    return 0;
}